Skip to main content

Remote Repositories

Remote Name

A remote name likde origin is an alias for a remote repository's URL.

Instead of typing the full remote URL every time, Git allows you to use a short name with easier reference.

Without a Remote Name

git push https://github.com/user/repo.git main

With a Remote Name

git push origin main

Remote Commands

  1. List all remote repo of local repo:
git remote -v

It return both fetch and push URLs.

  • Fetch URL: Used for pulling (downloading) changes from the remote repository.
  • Push URL: Used for pushing (uploading) changes to the remote repository.

By default, both are the same, but they can be different. For example, in some workflows:

  • You might fetch from a read-only URL (like git:// or SSH with limited access).
  • You might push to a different URL (like HTTPS with authentication).

Setting different URL:

  • --push flag is used to set a different push URL from the fetch URL
git remote set-url --push origin git@github.com:user/repo.git
  • the default url is fetch url
git remote set-url origin https://github.com/user/repo.git
  1. git remote add <name> <url> - Add a new remote repository
  2. git remote remove <name> - Remove remote repository
  3. git remote rename <old-name> <new-name> - Rename remote repository
  4. git remote show <name> - Show information about remote repository
  5. git remote set-url <name> <new-url> - Change remote URL
  6. git remote get-url <name> - Verify the remote URL

push Commands

It uploads your local commits to a remote repository. It syncs your local branch with a remote branch, allowing others to see and collaborate on your changes.

What Happens Under the Hood?

When you push:

  1. Git checks your local commits.
  2. It sends the changes to the remote repository.
  3. If successful, the remote branch updates to match your local branch.

If your remote branch has new commits, Git prevents you from pushing — you’ll need to pull and resolve conflicts first.

  1. git push - Pushes the changes from the current branch to its corresponding upstream branch.
  2. git push <remote> <branch> - pushes the specified branch to the remote.
  3. git push -u <remote> <branch> - Pushes the branch and sets it as the upstream branch for future git push and git pull commands.
  4. git push --force - Forces Git to push changes, even if it overwrites changes on the remote repository.
  5. git push --force-with-lease - Similar to --force, but only forces the push if no one else has updated the remote branch since your last fetch. This prevents accidental overwriting of others' work.
  6. git push --dry-run - Simulate Push Without Making Changes. Useful for verification before executing a real push.
  7. git push --delete <remote> <branch> - Remove a remote branch

fetch Commands

It downloads changes from the remote repository, but it doesn’t update your local working directory or current branch. It only updates your remote-tracking branches, allowing you to inspect the changes before integrating them.

It is used to retrieve the latest changes from a remote repository without merging them into your local repository. It allows you to update your local copy with new branches, tags, and commits available on the remote without modifying your working directory.

  1. git fetch - fetch from default remote, download commit, branches, tags but does not merge them into current branch.
  2. git fetch <remote> - fetch from specified remote.
  3. git fetch <remote> <branch> - fetch from specified branch.
  4. git fetch -all - update all configured remote when a repo has multiple remotes
  5. git fetch --dry-run - Simulates fetching without actually downloading anything.
  6. git fetch origin <commit-hash> - Fetches a specific commit from the remote.

pull Commands

It is used to fetch and merge changes from a remote repository into the current branch. It is essentially a combination of git fetch (downloads changes) and git merge (applies them to your branch).

  1. git pull - Fetches and merges changes from the default remote.
  2. git pull <remote> - Fetches and merges changes from specified remote.
  3. git pull <remote> <branch> - Pull form specified branch.
  4. git pull --rebase - Instead of merging changes, this command applies changes on top of your current branch, keeping the history clean.
  5. git pull --no-commit - Pulls changes but does not automatically create a merge commit.
  6. git pull --no-merge - Fetches updates but does not merge them automatically.

clone Commands

It create a copy of a remote repository on your local machine.

When you clone a repository, Git performs the following actions:

  • Copies the entire repository (all branches, commits, history, etc.) from the remote server to your local machine.

  • Sets up the remote connection (origin by default) so you can fetch, push, and pull changes.

  1. git clone <repository_url> <new_directory_name> - Clone the repo into a specific directory.
  2. git clone --branch <branch_name> <repository_url> - Clone a specific branch.
  3. git clone --depth 1 <repository_url> - If you only need the latest version of the repository and not the full commit history.